from pydantic import BaseModel, Field

from pydantic_settings import (
    BaseSettings,
    PydanticBaseSettingsSource,
    SettingsConfigDict,
    TomlConfigSettingsSource,
)


from hellchin.config.models import (
    ProjectConfig,
    # MySqlConfig
)

class MySqlConfig(BaseModel):
    """MySQL配置模型"""
    host: str = Field(None, description="Mysql数据库主机地址")
    port: int = Field(None, description="数据库端口")
    username: str = Field(None, description="数据库用户名")
    password: str = Field(None, description="数据库密码")
    database: str = Field(None, description="数据库名称")


class TomlConfigSettingsSourceMixin(BaseSettings):
    """A mixin that adds support for loading config from a TOML file."""

    @classmethod
    def settings_customise_sources(
            cls,
            settings_cls: type[BaseSettings],
            init_settings: PydanticBaseSettingsSource,
            env_settings: PydanticBaseSettingsSource,
            dotenv_settings: PydanticBaseSettingsSource,
            file_secret_settings: PydanticBaseSettingsSource,
    ) -> tuple[PydanticBaseSettingsSource, ...]:
        return (TomlConfigSettingsSource(settings_cls),)



class Settings(TomlConfigSettingsSourceMixin):
    # model_config = SettingsConfigDict(toml_file='dev.toml', extra='ignore')

    # project: ProjectConfig | None = Field(None, description="项目名称")
    mysql: MySqlConfig | None = Field(None, description="数据库配置")

    class Config:
        toml_file = 'dev.toml'
        extra = 'allow'      # 允许额外的配置项, 默认是'ignore', allow, ignore, strict

 
if __name__ == '__main__':
    settings = Settings()
    print(settings.model_dump())
    print(settings.mysql.model_dump())

